package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/
@RestController
@RequestMapping("/permission")
public class PermissionController {

	@Reference
	private PermissionService permissionService;

	@RequestMapping("/add")
	public Result add(@RequestBody Permission permission) {

		try {
			permissionService.add(permission);
			return new Result(true,"添加权限项成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(true,"添加权限项失败");
		}
	}

	@RequestMapping("/findAll")
	public PageResult findAll(@RequestBody QueryPageBean queryPageBean) {

		try {
			PageResult PageResult =  permissionService.findAll(queryPageBean);
			return PageResult;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}

	@RequestMapping("/delete")
	public Result delete(Integer id) {

		try {
			permissionService.delete(id);
			return new Result(true,"删除权限项成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(true,"删除权限项失败");
		}
	}


	@RequestMapping("/findById")
	public Result findById(Integer id) {

		try {
			Permission permission = permissionService.findById(id);
			return new Result(true,"查询权限项成功",permission);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(true,"查询权限项失败");
		}
	}

	@RequestMapping("/edit")
	public Result update(@RequestBody Permission permission) {

		try {
			permissionService.update(permission);
			return new Result(true,"修改权限项成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(true,"修改权限项失败");
		}
	}
}
